<template>
  <view>
    <!-- 搜索框区 -->
    <view class="componets-search">
      <search></search>
    </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="swipper">
      <swiper-item v-for="(item,i) in swipperList" :key="i" class="swipper-item">
        <navigator :url="'/subpkg/goods-list/goods-list?goods_id=' + item.goods_id" class="swipper-item">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航区域 -->
    <view class="navList">
      <view v-for="(item,index) in navList" :key="index" @click="gotoCate(index)">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>
    <!-- 楼层区 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item,j) in floorList" :key="j">
        <!-- 楼层标题区 -->
        <image :src="item.floor_title.image_src"></image>
        <!-- 楼层图片区 -->
        <view class="floor-box">
          <!-- 左侧大图片 -->
          <navigator class="floor-box-left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" mode=""
              :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
          </navigator>
          <!-- 右侧小图片 -->
          <view class="floor-box-right">
            <template v-for="(item2, i2) in item.product_list">
              <navigator :url="item2.url" v-if="i2 !== 0">
                <image :src="item2.image_src" mode="widthFix" v-if="i2 !== 0"
                  :style=" {width: item2.image_width + 'rpx'}"></image>
              </navigator>
            </template>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  import search from '@/componets/search/search.vue'
  export default {
    data() {
      return {
        // 轮播图数据
        swipperList: [],
        // 导航栏数据
        navList: [],
        // 楼层数据
        floorList: []
      }
    },
    methods: {
      // 获取轮播图数据
      async getSwipper() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status !== 200) return uni.$showMsg
        this.swipperList = res.message
      },
      // 获取导航数据
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg
        this.navList = res.message
      },
      // 跳转到分类页
      gotoCate(index) {
        if (index === 0) {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg
        // 通过双层 forEach 循环，处理 URL 地址
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            // 以 ？ 分割字符串，拿到后面的 query=""
            prod.url = '/subpkg/goods-list/goods-list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      // 去往商品列表页
      gotoGoodsList(item3) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
        })
      }
    },
    // 页面加载时
    onLoad() {
      this.getSwipper(),
        this.getNavList(),
        this.getFloorList()
    },
    components: {
      search
    },
    mixins: [badgeMix]
  }
</script>

<style>
  .swipper {
    height: 320rpx;
  }

  .swipper-item,
  .swipper-item image {
    width: 100%;
    height: 100%;
  }

  .navList {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin: 15rpx;
  }

  .navList image {
    width: 120rpx;
    height: 140rpx;
  }

  .floor-list {
    display: flex;
    flex-direction: column;
    margin: 10rpx;
  }

  .floor-item>image {
    height: 60rpx;
  }

  .floor-box-right {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .floor-box-right>image {}

  .floor-box {
    display: flex;
    flex-direction: row;
  }

  .componets-search {
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>
